@extends('layouts.admin')

@section('content')
    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal">图片编辑</button>
    <button id="delete-btn" class="btn btn-danger" data-url="{{route('article.destroy',$article)}}">删除</button>
    <a href="/article/publicToWeibo/{{$article->id}}" class="btn btn-info">发布到微博</a>
    <a href="/article/publicToToutiao/{{$article->id}}" class="btn btn-warning">发布到头条</a>

    {!! Form::model($article,['route' => ['article.update',$article],'method'=>'PUT','class'=>'form-horizontal']) !!}
    @include('articles._formArticle')
    {!!  Form::close()  !!}



    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel">封面图片</h4>
                </div>
                <div class="modal-body">
                    {{--图片上传--}}
                    <img src="{{$article->coverage_url}}" width="100%" class="img-responsive">
                    <h2 class="text-center">文章的全部图片</h2>
                    {{--设置封面--}}
                    @foreach($article->images as $img)
                        <img src="{{$img->imgURL()}}" width="100%" class="img-responsive">
                        <a data-image-id="{{$img->id}}" class="btn btn-xs btn-danger image-delete">删除</a>
                        <a data-image-id="{{$img->id}}" data-article-id="{{$article->id}}" class="btn btn-xs btn-success set-coverage">设置封面</a>
                        <hr>
                    @endforeach
                    {{--图片删除--}}
                    {{--视频预览--}}
                </div>
            </div>
        </div>
    </div>



@endsection

@section('scripts')

    <script type="text/javascript">

        $('#delete-btn').click(function (event) {
            var url = $(event.target).data('url');
            $.ajax({
                url: url, type: 'delete', success: function () {
                    location.href = '/article';
                }
            });
        });
        //删除图片
        $('a.image-delete').click(function () {
            if (confirm('确定要删除?')) {
                var imageId = $(this).data('image-id')
                var url = '/image/' + imageId;
                $.ajax({
                    url: url,
                    type: 'delete',
                    success: function (response) {
                        location.reload();
                    }
                });
            }

        });
        //var articleId = '{{$article->id}}';
        //设置封面
        $('a.set-coverage').click(function () {
            var data = {articleId: $(this).data('article-id'), imageId: $(this).data('image-id')};
            $.post('/article/set-coverage', data, function (response) {
                location.reload();
            })
        });


        $('#tag_list').select2({
            placeholder: 'choose a tag',
        });
        $('#show_list').select2({
            placeholder: '请选择一个美剧',
        });
        $('#movie_list').select2({
            placeholder: '请选择一个美剧',
        });
    </script>

@endsection
